package com.gh.controller.api;


import com.gh.common.exception.Assert;
import com.gh.common.result.R;
import com.gh.common.result.ResponseEnum;
import com.gh.service.UserInfoService;
import com.gh.utils.JwtUtils;
import com.gh.vo.LoginVo;
import com.gh.vo.RegisterVo;
import com.gh.vo.UserIndexVo;
import com.gh.vo.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户基本信息 前端控制器
 * </p>
 *
 * @author Eric
 * @since 2021-09-06
 */
@Api(tags = "用户基本信息")
@Slf4j
@RestController
@RequestMapping("/api/core/user-info")
public class UserInfoController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private UserInfoService userInfoService;

    @ApiOperation("会员注册")
    @PostMapping("/register")
    public R register(@RequestBody RegisterVo registerVo) {
        //校验验证码是否成功
        String phone = registerVo.getMobile();
        String code = (String) redisTemplate.boundValueOps(phone).get();
        if(StringUtils.isBlank(code)){
            return R.error().message("手机号不存在");
        }
        if(!code.equalsIgnoreCase(registerVo.getCode())){
            return R.error().message("验证码错误");
        }
        //注册
        userInfoService.register(registerVo);
        return R.ok();
    }

    @ApiOperation("会员登录")
    @PostMapping("/login")
    public R login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();
        /**
         * 判断手机号和密码不能为空
         */
        Assert.notEmpty(mobile, ResponseEnum.MOBILE_NULL_ERROR);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_NULL_ERROR);
        String ip = request.getRemoteAddr();
        UserInfoVo userInfoVo = userInfoService.login(loginVo, ip);
        return R.ok().data("userInfo", userInfoVo);
    }

    @ApiOperation("校验令牌")
    @GetMapping("/checkToken")
    public R checkToken(HttpServletRequest request) {
        String token = request.getHeader("token");
        boolean flag = JwtUtils.checkToken(token);
        if (flag) {
            return R.ok();
        }
        return R.setResult(ResponseEnum.LOGIN_AUTH_ERROR);
    }

    @ApiOperation("校验手机号是否注册")
    @GetMapping("checkMobile/{mobile}")
    public R checkMobile(@PathVariable String mobile){
        boolean result = userInfoService.checkMobile(mobile);
        return R.ok().data("isExist",result);
    }

    @ApiOperation("获取个人空间用户信息")
    @GetMapping("auth/getIndexUserInfo")
    public R getIndexUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        Long userId = JwtUtils.getUserId(token);
        UserIndexVo userIndexVo = userInfoService.getIndexUserInfo(userId);
        return R.ok().data("userIndexVo",userIndexVo);
    }
}

